/*
 * Copyright 2008 Max Kugland
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.splink.library.utils {

	/**
	 * Utility methods to manipulate arrays.
	 * 
	 * @author Max Kugland
	 */
	public class ArrayUtils {

		/**
		 * Checks whether the contents of the given arrays are equal
		 * 
		 * @param a1 array1		 * @param a2 array2
		 * @return true if the contents are equal, otherwise return false
		 */
		public static function eq(a1 : Array, a2 : Array) : Boolean {
			if(a1 == a2) return true;
			if(a1.length != a2.length) return false;
			
			for (var i : int = 0;i < a1.length;i++) {
				if(a1[i] != a2[i]) return false;	
			}
			
			return true;
		}

		/**
		 * Checks whether an array contains a specific item. If you need strict equality (===) 
		 * use array.indexOf() instead.
		 * 
		 * @param ar array to check
		 * @param element element to check
		 * @return true if the array contains the element otherwise returns false
		 */
		public static function contains(ar : Array, item : *) : Boolean {
			if(getItemIndex(ar, item) != -1) {
				return true;
			}
			return false;
		}

		/**
		 * Removes duplicate elements from the given array.
		 * 
		 * @param array to process
		 */
		public static function removeDuplicates(ar : Array) : Array {
			var a1 : int;
			var a2 : int;
			for (a1 = 0;a1 < ar.length;a1++) {
				for (a2 = 0;a2 < ar.length;a2++) {
					if (ar[a2] == ar[a1]) {
						if (a2 != a1) {
							ar.splice(a2, 1);
						}
					}
				}
			} 
			return ar;
		}

		/**
		 * Shuffles a given array
		 * 
		 * @param ar array to be shuffled
		 * @return shuffled array 
		 */
		public static function shuffle(ar : Array) : Array {
			var n : int;
			var tmp : *;
			var len : int = ar.length;
			for (var i : int = 0;i < len;i++) {
				n = Math.floor(Math.random() * len);
				tmp = ar[i];
				ar[i] = ar[n];
				ar[n] = tmp;
			}
			return ar;
		}

		/**
		 * Removes an item from an array. If you need strict equality (===) use array.indexOf() 
		 * instead. If the item was not removed from the array, <code>null</code> is returned 
		 * 
		 * @param ar the array to remove the item from
		 * @param item the item to remove
		 * @return the removed item
		 */
		public static function remove(ar : Array, item : *) : * {
			var index : int = getItemIndex(ar, item);
			if(index != -1)	{
				ar.splice(index, 1);
				return item;
			}
			return null;
		}

		/**
		 * Computes the index of the specifyed item within the given Array. If the item
		 * can not be found, -1 is returned.
		 * 
		 * @param ar the Array containing the given item
		 * @param item the item to compute the index for
		 * @return the index of the item
		 */
		public static function getItemIndex(ar : Array, item : *) : int {
			var i : int = ar.length;
			while(--i > -1) {
				if(ar[i] == item) {
					return i;
				}
			}
			return -1;
		}
	}
}
